package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.PotentialCustomer;
import cn.wolfcode.crm.query.PotentialCustomerQueryObject;
import cn.wolfcode.crm.service.IPotentialCustomerService;
import cn.wolfcode.crm.util.BecameDataLoad;
import cn.wolfcode.crm.util.JsonResult;
import cn.wolfcode.crm.util.PageResult;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("potentialCustomer")
public class PotentialCustomerController {

    @Autowired
    private IPotentialCustomerService potentialCustomerService;

    @RequestMapping("index")
    public String index(){
        return "potentialCustomer/list";
    }

    @ResponseBody
    @RequestMapping("list")
    public PageResult index(PotentialCustomerQueryObject qo){
        PageResult result = potentialCustomerService.queryListByQo(qo);
        return result;
    }

    @ResponseBody
    @RequestMapping("save")
    public JsonResult save(PotentialCustomer potentialCustomer){
        JsonResult result = new JsonResult();
        try {
            potentialCustomerService.insert(potentialCustomer);
        }catch (Exception e){
            e.printStackTrace();
            result = new JsonResult("保存出错!");
        }
        return result;
    }

    @ResponseBody
    @RequestMapping("update")
    public JsonResult update(PotentialCustomer potentialCustomer){
        JsonResult result = new JsonResult();
        try {
            potentialCustomerService.updateByPrimaryKey(potentialCustomer);
        }catch (Exception e){
            e.printStackTrace();
            result = new JsonResult("编辑出错!");
        }
        return result;
    }

    @ResponseBody
    @RequestMapping("delete")
    public JsonResult delete(Long id){
        JsonResult result = new JsonResult();
        try {
            if (id!=null){
                potentialCustomerService.deleteByPrimaryKey(id);
            }
        }catch (Exception e){
            e.printStackTrace();
            result = new JsonResult("删除出错!");
        }
        return result;
    }

    @ResponseBody
    @RequestMapping("queryPotentialCustomerByid")
    public PotentialCustomer queryPotentialCustomerByid(Long id){
        return potentialCustomerService.selectByPrimaryKey(id);
    }

    @ResponseBody
    @RequestMapping("queryAllPotentialCustomer")
    public List<PotentialCustomer> queryAllPotentialCustomer(){
        return potentialCustomerService.selectAll();
    }

    //考试页面需要用到
    @ResponseBody
    @RequestMapping("queryForList")
    public List<PotentialCustomer> queryForList(){
        return potentialCustomerService.selectAll();
    }


    //根据Id修改状态
    @ResponseBody
    @RequestMapping("updateStateById")
    public JsonResult updateStateById(PotentialCustomer potentialCustomer){
        JsonResult result = new JsonResult();
        try {
            potentialCustomerService.updateStateById(potentialCustomer);
        }catch (Exception e){
            e.printStackTrace();
            result = new JsonResult("编辑出错!");
        }
        return result;
    }

    //查看客户资源池页面
    @RequestMapping("resourcePool")
    public String resourcePool(PotentialCustomerQueryObject qo){
        return "potentialCustomer/resourcePool";
    }

    //查找客户资源池列表
    @ResponseBody
    @RequestMapping("resourcePoolList")
    public PageResult resourcePoolList(PotentialCustomerQueryObject qo){
        PageResult result = potentialCustomerService.queryResourcePoolListByQo(qo);
        return result;
    }

    //保存指派信息----仅仅只改变营销人员信息与状态
    @ResponseBody
    @RequestMapping("updateMarketingPersonAndStateById")
    public JsonResult updateMarketingPersonAndStateById(PotentialCustomer potentialCustomer){
        JsonResult result = new JsonResult();
        try {
            potentialCustomerService.updateMarketingPersonAndStateById(potentialCustomer);
        }catch (Exception e){
            e.printStackTrace();
            result = new JsonResult("编辑出错!");
        }
        return result;
    }

    //根据学校Id查询该学校有多少潜在学员
    @ResponseBody
    @RequestMapping("queryPotentialCustomerNumberBySchoolId")
    public int queryPotentialCustomerNumberBySchoolId(Long id){
        return potentialCustomerService.queryPotentialCustomerNumberBySchoolId(id);
    }


    //潜在学员转正操作
    @ResponseBody
    @RequestMapping("become")
    public JsonResult become(PotentialCustomer potentialCustome, BecameDataLoad becameDataLoad){
        JsonResult result = new JsonResult();
        try {
            potentialCustomerService.become(potentialCustome,becameDataLoad);
        }catch (Exception e){
            e.printStackTrace();
            result = new JsonResult("转正出错!");
        }
        return result;
    }

    @RequestMapping("exportExcel")
    public void exportExcel(HttpServletResponse response) throws Exception{
        //导出
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmmss");
        String fileName = sdf.format(new Date()) + ".xls";
        response.setContentType("application/x-excel");
        response.setCharacterEncoding("UTF-8");
        response.addHeader("Content-Disposition", "attachment;filename="
                + fileName);
        List<PotentialCustomer> list = new ArrayList<>();
        List<PotentialCustomer> potentialCustomers = potentialCustomerService.selectAll();
        for (PotentialCustomer potentialCustomer : potentialCustomers) {
            list.add(potentialCustomer);
        }
        //1.创建工作薄
        WritableWorkbook workbook = Workbook.createWorkbook(response.getOutputStream());
        //2.创建页签
        WritableSheet sheet = workbook.createSheet("员工列表", 0);
        //3.创建单元格
        String[] header = new String[]{"姓名","性别","电话","微信","QQ","意向校区","意向班级","意向程度","建档日期","营销人员","状态","备注"};
        Label label = null;
        for (int i = 0; i < header.length; i++) {
            label = new Label(i,0,header[i]);
            //4.添加到页签中
            sheet.addCell(label);
        }
        //迭代数据
        for (int i = 1; i <list.size()+1 ; i++) {
            label = new Label(0,i,list.get(i-1).getName());
            sheet.addCell(label);
            label = new Label(1,i,list.get(i-1).getGender());
            sheet.addCell(label);
            label = new Label(2,i,list.get(i-1).getPhone());
            sheet.addCell(label);
            label = new Label(3,i,list.get(i-1).getWeixin());
            sheet.addCell(label);
            label = new Label(4,i,list.get(i-1).getQq());
            sheet.addCell(label);
            if (list.get(i-1).getIntentionCampus()==null){
                label = new Label(5,i,"");
                sheet.addCell(label);
            }else {
                label = new Label(5,i,list.get(i-1).getIntentionCampus().getName());
                sheet.addCell(label);
            }
            if (list.get(i-1).getIntentionClass()==null){
                label = new Label(6,i,"");
                sheet.addCell(label);
            }else {
                label = new Label(6,i,list.get(i-1).getIntentionClass().getName());
                sheet.addCell(label);
            }
            if (list.get(i-1).getIntentionDegree()==null){
                label = new Label(7,i,"");
                sheet.addCell(label);
            }else {
                label = new Label(7,i,list.get(i-1).getIntentionDegree().getName());
                sheet.addCell(label);
            }
            if (list.get(i-1).getCreateTime()==null){
                label = new Label(8, i, "");
                sheet.addCell(label);
            }else {
                sdf = new SimpleDateFormat("yyyy-MM-dd");
                String time = sdf.format(list.get(i - 1).getCreateTime());
                label = new Label(8, i, time);
                sheet.addCell(label);
            }
            if (list.get(i-1).getMarketingPerson()==null){
                label = new Label(9,i,"");
                sheet.addCell(label);
            }else {
                label = new Label(9,i,list.get(i-1).getMarketingPerson().getUsername());
                sheet.addCell(label);
            }
            label = new Label(10, i, list.get(i - 1).getState().toString());
            sheet.addCell(label);
            label = new Label(11, i, list.get(i - 1).getRemark());
            sheet.addCell(label);
        }
        //5.写出数据
        workbook.write();
        workbook.close();
    }
}
